package xmltest;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.util.List;

public class GameReaderTest {
    public static void main(String[] args) throws DocumentException {
        //创建解析器
        SAXReader saxReader = new SAXReader();

        //创建一个文档对象
        Document document = saxReader.read("day14-code/src/xml/Game.xml");
        //获取根元素
        Element rootElement = document.getRootElement();
        System.out.println("根标签名称为：" + rootElement.getName());
        //获取根元素下的所有子元素
        List<Element> elements = rootElement.elements();
        for (Element element : elements) {
            //获取子元素名称和属性值
            System.out.println("\t子标签的名称为："+element.getName() + "子标签的id属性为：" + element.attributeValue("id"));
            Element name = element.element("name");
            System.out.println("\t\t孙子标签名为：" + name.getName() + "\n\t\t游戏名为：" + name.getText());
            Element company = element.element("company");
            System.out.println("\t\t孙子标签名为：" + company.getName() + "\n\t\t公司名为：" + company.getText());
        }
    }
}
